from sqlalchemy import Column, ForeignKey, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import backref, relation
from divirt import sqlalchemy_meta_data
from divirt import logging
from nic import Nic
from tap import Tap

log = logging.getLogger('divirt.components.tapconn')

Base = declarative_base(metadata=sqlalchemy_meta_data)
class TapConn(Base):
    # Table
    __tablename__ = 'tapconns'
    id = Column(Integer, primary_key=True)
    nic_id = Column(Integer, ForeignKey('nics.id'))
    tap_id = Column(Integer, ForeignKey('taps.id'))
    
    # Relations
    nic = relation(Nic, backref=backref('tapconn'))
    tap = relation(Tap, backref=backref('tapconn'))

    def __init__(self, nic_id=None, tap_id=None):
        self.nic_id = nic_id
        self.tap_id = tap_id

    def __repr__(self):
        return '<TapConn(id=%s, nic_id=%s, tap_id=%s)>' \
            % (self.id, self.nic_id, self.tap_id)
